Title: SQL W3S (By-BKK) / 111 Foreign Key
Tags: Obsidian_to_Anki

Q: What does the FOREIGN KEY constraint do in SQL?
A: It prevents actions that would destroy links between tables by ensuring that the values in the foreign key column match values in the referenced table’s primary key

Q: True or False: The FOREIGN KEY refers to the PRIMARY KEY in another table
A: True

Q: What is a FOREIGN KEY in SQL?
A: A field or collection of fields in one table that refers to the PRIMARY KEY in another table

Q: What is the table containing the FOREIGN KEY called?
A: Child table

Q: What is the table containing the referenced PRIMARY KEY called?
A: Referenced table or parent table

Q: Give an example of a FOREIGN KEY relationship
A: The "PersonID" column in the "Orders" table refers to the "PersonID" column (PRIMARY KEY) in the "Persons" table

Q: Why does the FOREIGN KEY constraint prevent invalid data?
A: Because the value in the foreign key column must exist in the parent table’s primary key column

Q: How do you declare a FOREIGN KEY constraint on table creation (MySQL)?
A: Use FOREIGN KEY(column) REFERENCES parentTable(column) in CREATE TABLE statement

Q: How do you declare a FOREIGN KEY constraint on table creation (SQL Server/Oracle/MS Access)?
A: FOREIGN KEY REFERENCES parentTable(column) in CREATE TABLE statement

Q: How can you name a FOREIGN KEY constraint?
A: Use CONSTRAINT constraintName FOREIGN KEY(column) REFERENCES parentTable(column)

Q: How do you add a FOREIGN KEY to an existing table?
A: Use ALTER TABLE tableName ADD FOREIGN KEY(column) REFERENCES parentTable(column)

Q: How do you name and add a FOREIGN KEY using ALTER TABLE?
A: ALTER TABLE tableName ADD CONSTRAINT constraintName FOREIGN KEY(column) REFERENCES parentTable(column)

Q: How do you remove a FOREIGN KEY constraint in MySQL?
A: ALTER TABLE tableName DROP FOREIGN KEY constraintName

Q: How do you remove a FOREIGN KEY constraint in SQL Server/Oracle/MS Access?
A: ALTER TABLE tableName DROP CONSTRAINT constraintName

Q: Preventing actions that destroy table links in SQL is done by which constraint?
A: FOREIGN KEY constraint

Q: FOREIGN KEY ensures child table column values exist in parent table’s primary key
A: What does the FOREIGN KEY constraint guarantee in SQL?

Q: A primary purpose of FOREIGN KEY is to maintain referential integrity
A: What does the FOREIGN KEY constraint help to maintain in the database?

Q: The FOREIGN KEY must match values in the referenced table
A: True or False: The FOREIGN KEY can contain values not present in the referenced table

Q: Referential integrity is enforced by FOREIGN KEY constraint
A: How is referential integrity enforced in SQL tables?

Q: Child table
A: What is the name for the table containing the foreign key?

Q: Parent table
A: What is another term for the table containing the referenced primary key?[1]